const { autoCommit } = require("oracledb")
const db = require("../../lib/conn_oracle")
const {logoutSession , addLog , manyLogoutSession, manyAddLog} = require("../../util/loginAndSession")

exports.getUserSessions = async (req,res)=>{
    // console.log(req.body)
    let {form} = req.body
    let {isActive} = form
    let resDatas
    let sessionsSql = `select * from c##wxw.vue_user_sessions where is_active = :isActive`
    let userSessions = await db.search(sessionsSql,[isActive])
    resDatas = {userSessions}
    res.json({
        status:1,
        msg:"查询成功",
        resDatas
    })

}

exports.deleteUserSession = async (req,res)=>{
    // console.log(req.body);
    let {form} = req.body
    let {SESSION_ID,USER_ID,CLIENT_IP} = form 
    let killSessionData = [SESSION_ID]
    let addLogData = [USER_ID,CLIENT_IP,"KILL","SUCCESS"]
    let killSession = await logoutSession(killSessionData)
    addLog(addLogData)    
    if(killSession.rowsAffected === 1 ){     
        res.json({
            status:1,
            msg:"清理成功",  
        }) 
    }
}

exports.batchDeleteUserSession = async (req,res)=>{
    let {form} = req.body
    // console.log(form);    
    let batchSessionsArr = []    
    let batchAddLogArr = []
    form.forEach(item=>{
        let sessionids = []
        let lgoninlogs = []
        sessionids.push(item.SESSION_ID)
        lgoninlogs.push(item.USER_ID,item.CLIENT_IP,"KILL","SUCCESS")
        batchSessionsArr.push(sessionids)
        batchAddLogArr.push(lgoninlogs)
    })
    // console.log(batchSessionsArr)
    // console.log(batchAddLogArr)
    try {
        let batchSessions = await manyLogoutSession(batchSessionsArr)
        manyAddLog(batchAddLogArr) 
        if(batchSessions.rowsAffected === form.length ){
            res.json({
                status:1,
                msg:"批量清理成功",
            })
        }
    }catch(err){
        console.log(err);
    }

}